package com.zjl.学习.第01章_入门;


import com.zjl.springbatch.util.OutColour;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.scope.context.StepContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.core.task.SimpleAsyncTaskExecutor;

import java.nio.charset.Charset;
import java.util.Map;

/**
 * 任务默认正常执行完后，会根据 Job 名字 存入数据库，下次就不会执行了
 * 默认只会执行一次，因为没有设定可多次执行
 *
 * @see org.springframework.boot.autoconfigure.batch.JobLauncherApplicationRunner
 *  的这个 executeLocalJobs 方法 就是执行的原理
 *
 */
@SpringBootApplication
@EnableBatchProcessing//开启spring batch
public class F_最简单Batch案例 {

    public static void main(String[] args) {
        args = new String[]{"name=test"};
        System.setProperty("spring.profiles.active","01");//  同理 --spring.profiles.active=xxx

        SpringApplication.run(F_最简单Batch案例.class, args);
        System.out.println("***********启动完毕**当前编码:" + Charset.defaultCharset().name() + "****************");

    }
    @Autowired//@EnableBatchProcessing 这个注解帮你从 spring batch 的配置文件中加载了
    private JobLauncher jobLauncher;//Job 启动器

    @Autowired//Job 构造工厂
    private JobBuilderFactory jobBuilderFactory;
    @Autowired//构建 Step
    private StepBuilderFactory stepBuilderFactory;
    @Bean
    public Step step001() throws Exception {
        return stepBuilderFactory
                .get("step001")//Step 工作的名称
                .tasklet(tasklet001())//放入工作内容
                .build();//构建
    }
    @Bean//任务 Tasklet 这是最简单的    还有Chunk块处理
    public Tasklet tasklet001(){
        return new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                StepContext stepContext = chunkContext.getStepContext();//获取 Job的 入参，配置 执行者等
                Map<String, Object> jobParameters = stepContext.getJobParameters();//获取 Job的 参数信息 JobParameters
                OutColour.out.printlnRed("Hello SpringBatch 初步使用成功....Job的参数：" + jobParameters);
                return RepeatStatus.FINISHED;//已完成
            }
        };
    }
    @Bean
    public Job job001() throws Exception {
        return jobBuilderFactory
                .get("job001")//job 工作的名称
                .start(step001())//放入 step
                .build();//构建
    }
}
